package liurong.driver.stream;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlTest {
	public static void main(String[] args) throws Exception {
		// DocumentBuilderFactory factory =
		// DocumentBuilderFactory.newInstance();
		// DocumentBuilder builder = factory.newDocumentBuilder();
		// Document document = builder.parse(xml);
		// Element element = document.getDocumentElement();
		//
		// NodeList nodeList = element.getElementsByTagName("stringid");
		//
		// for (int i = 0; i < nodeList.getLength(); i++) {
		// Node item = nodeList.item(i);
		// NamedNodeMap attributes = item.getAttributes();
		// for (int j = 0; j < attributes.getLength(); j++) {
		// Node item2 = attributes.item(j);
		// if (item2.getNodeName().equals("en")) {
		// System.out.println(item2.getNodeValue());
		// }
		// }
		// }

		// dom4j加xpath
		String xml = "D:/jack/02/res/FMLang/FMLang.xml";
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(xml));
		Element node = (Element) document.selectSingleNode("//stringid[@en='Chat']");
		Attribute attribute = node.attribute("jap");
		System.out.println(attribute.getData());
		attribute.setData("jap");
		System.out.println(node.asXML());

		// 指定文件输出的位置
		FileOutputStream out = new FileOutputStream("D:/jack/02/res/FMLang/FMLang1.xml");
		// 1.创建写出对象
		XMLWriter writer = new XMLWriter(out);
		writer.write(document);
		writer.close();
	}
}
